package com.gxwy.yonghuziliao.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gxwy.yonghuziliao.form.YongHuZiLiao;

@Repository
public class YongHuZiLiaoDaoImpl implements YongHuZiLiaoDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")

	public List<YongHuZiLiao> findAll(String username) {
		return sessionFactory.getCurrentSession().createQuery("from YongHuZiLiao where zhanghuhuming=?").setString(0, username).list();
	}

	public void addYongHuZiLiao(YongHuZiLiao yhzl) {
	  
		sessionFactory.getCurrentSession().save(yhzl);
		
	
	}

	public void deleteYongHuZiLiao(int id) {
		sessionFactory.getCurrentSession().createQuery("delete YongHuZiLiao y where y.id=?").setInteger(0, id).executeUpdate();
		
	}

	public void updateBieMing(String bieming,int id) {
		sessionFactory.getCurrentSession().createQuery("update YongHuZiLiao y set y.zhanghubieming=? where y.id=?").setString(0, bieming).setInteger(1,id).executeUpdate();
		
	}

	public YongHuZiLiao findById(int id) {
		String hql="from YongHuZiLiao y where y.id=?";
		return (YongHuZiLiao) sessionFactory.getCurrentSession().createQuery(hql).setInteger(0,id).uniqueResult();
	}

	public void guashi(int id) {
		sessionFactory.getCurrentSession().createQuery("update YongHuZiLiao y set y.jinjiguashi=1 where y.id=?").setInteger(0, id).executeUpdate();
		
	}

	
	

	public void Delete(Integer id) {
		// TODO Auto-generated method stub
		YongHuZiLiao contact = (YongHuZiLiao) sessionFactory.getCurrentSession().load(
				YongHuZiLiao.class, id);
		if (null != contact) {
			sessionFactory.getCurrentSession().delete(contact);
		}
	}

	public void updateYinHangMiMa(YongHuZiLiao yhzl) {
		sessionFactory.getCurrentSession().update(yhzl);
		
	}

	public YongHuZiLiao findByZhangHao(String yhzh) {
		String hql="from YongHuZiLiao y where y.yinhangzhanghao=?";
		return (YongHuZiLiao) sessionFactory.getCurrentSession().createQuery(hql).setString(0,yhzh).uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public List<YongHuZiLiao> list(String accounts) {
		// TODO Auto-generated method stub
		 return sessionFactory.getCurrentSession().createQuery
					("from  YongHuZiLiao where yinhangzhanghao='"+accounts+"'").list();
	}

	public void Update(YongHuZiLiao contact, Integer id) {
		// TODO Auto-generated method stub
	String hql="update YongHuZiLiao  set jinjiguashi=?,kaihujigou=?,yinhangmima=?,yinhangzhanghao=?,zhanghubieming=?,zhanghuhuming=?,zhanghuleixing=?,zhengjianhaoma=?,zhengjianleixing=? where id=?";
		
		Query query=(Query) sessionFactory.getCurrentSession().createQuery(hql);
		((org.hibernate.Query) query).setString(0,contact.getJinjiguashi());
		query.setString(1, contact.getKaihujigou());
		query.setString(2, contact.getYinhangmima());
		query.setString(3,contact.getYinhangzhanghao());
		query.setString(4, contact.getZhanghubieming());
		query.setString(5,contact.getZhanghuhuming());
		query.setString(6,contact.getZhanghuleixing());
		query.setString(7, contact.getZhengjianhaoma());
		query.setString(8,contact.getZhanghuleixing());
		query.setInteger(9, id);
		query.executeUpdate();
	}

}
